<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

class categories extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $this->load->helper('url');
        echo( $this->load->view('menu', '', true) ); // Necessary for now because, for some reason, if you just do $this->load->view('menu'), it is printed to the HTML after all the below echo statements.

        $wp_terms = $this->db->get('wp_terms');
        $term_taxonomy_categories = $this->db->get_where('wp_term_taxonomy', array('taxonomy' => 'category'));
        foreach( $term_taxonomy_categories->result() as $cat_row)
        {
            foreach( $wp_terms->result() as $term_row ) // There's probably a better way to do this than looping through the whole collection of terms and only using one element.
            {
                if( $term_row->term_id == $cat_row->term_id )
                {
                    echo $cat_row->term_taxonomy_id.': '.anchor('categories/single/'.$cat_row->term_taxonomy_id, $term_row->name).'<br/>';
                }
            }
        }
//         echo '<br/>Function <em>index()</em> is done.';
    }
    function single()
    {
        $this->load->helper('url');
        echo( $this->load->view('menu', '', true) ); // Necessary for now because, for some reason, if you just do $this->load->view('menu'), it is printed to the HTML after all the below echo statements.
        if( $this->uri->segment(3) === FALSE)
        {
            die('Need a post ID in URI segment 3.');
        }

        $term_relationships = $this->db->get_where('wp_term_relationships', array('term_taxonomy_id' => $this->uri->segment(3)));
        if( $term_relationships->num_rows() == 0)
        {
            die('No posts in this category.');
        }
        foreach( $term_relationships->result() as $relate_row )
        {
            $this->db->or_where('ID', $relate_row->object_id);
        }
        $this->db->where(array('post_status' => 'publish'));
        $posts = $this->db->get('wp_posts');

        foreach( $posts->result() as $row)
        {
            echo '<h2>'.$row->post_title.'</h2>';
            echo '<p>The content would go here. But that would take up space. Instead, see the post '.anchor('posts/single/'.$row->ID, 'here', 'title="'.$row->post_date.'"').'.</p>';
            //echo $row->post_content.'<br/>';
        }
    }
    function formatted_single()
    {
        $this->load->helper('url');
        echo( $this->load->view('menu', '', true) ); // Necessary for now because, for some reason, if you just do $this->load->view('menu'), it is printed to the HTML after all the below echo statements.
        if( $this->uri->segment(3) === FALSE)
        {
            die('Need a post ID in URI segment 3.');
        }

        echo( $this->load->view('us', '', true) );
        die();
        
        $term_relationships = $this->ls->get_term_relationships($this->uri->segment(3));
        foreach( $term_relationships->result() as $relate_row )
        {
            $this->db->or_where('ID', $relate_row->object_id);
        }
        $this->db->where(array('post_status' => 'publish'));
        $posts = $this->db->get('wp_posts');

        foreach( $posts->result() as $row)
        {
            echo '<h2>'.$row->post_title.'</h2>';
            echo '<p>The content would go here. But that would take up space. Instead, see the post '.anchor('posts/single/'.$row->ID, 'here', 'title="'.$row->post_date.'"').'.</p>';
            //echo $row->post_content.'<br/>';
        }
    }
    function other()
    {
      $this->load->database();
      $this->db->where('post_status' , 'publish');
      $this->db->order_by('post_date', 'desc');
      $data['list_query'] = $this->db->get('wp_posts',4);
      $this->db->where('ID', $this->uri->segment(3,60));
      $data['main_query'] = $this->db->get('wp_posts', 1);
      $this->load->view('other', $data);
    }
    function google()
    {
      echo( $this->load->view('google', '', true) );
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */